package com.jeecms.cms.entity.assist;

import com.jeecms.cms.entity.assist.base.BaseBranch;
import com.jeecms.common.hibernate4.HibernateTree;
import com.jeecms.common.hibernate4.PriorityInterface;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * 组织架构实体类
 */
public class Branch extends BaseBranch implements HibernateTree<Integer>, PriorityInterface, Cloneable {

    public Branch() {
    }

    public Branch(Integer id) {
        super(id);
    }

    public JSONObject convertToJson(boolean showChild) {
        JSONObject json = new JSONObject();
        if (getId() != null) {
            json.put("id", getId());
        } else {
            json.put("id", "");
        }
        if (getPriority()!=null) {
            json.put("priority", getPriority());
        } else {
            json.put("priority", "");
        }
        json.put("remarks", getRemarks());
        //json.put("deep", getDeep());
        if (StringUtils.isNotBlank(getName())) {
            json.put("name", getName());
        } else {
            json.put("name", "");
        }

        json.put("nodeIds", getNodeIds());
        if (getSite()!=null && StringUtils.isNotBlank(getSite().getName())) {
            json.put("siteName", getSite().getName());
        }else{
            json.put("siteName", "");
        }
        if (getSite()!=null && getSite().getId()!=null) {
            json.put("siteId", getSite().getId());
        }else{
            json.put("siteId", "");
        }

        if (getParent() != null) {
            json.put("parentId", getParent().getId());
            json.put("parentName", getParent().getName());
        }else{
            json.put("parentId", "");
            json.put("parentName", "");
        }
        if (getChild() != null) {
            json.put("childCount", getChild().size());
        } else {
            json.put("childCount", "");
        }
        if (getTopBranch() != null) {
            json.put("topId", getTopBranch().getId());
            json.put("topName", getTopBranch().getName());
        }else{
            json.put("topId", "");
            json.put("topName","");
        }
        if(showChild){
            getChildJson(json, getChild(), showChild);
        }
        return json;
    }

    private JSONObject getChildJson(JSONObject parent,Set<Branch> branches, boolean showChild){
        JSONArray childArray=new JSONArray();
        Iterator<Branch>it=branches.iterator();
        int i=0;
        while(it.hasNext()) {
            Branch b = it.next();
            childArray.put(i++,b.convertToJson(showChild));
            if(b.getChild()!=null&&b.getChild().size()>0){
                getChildJson(b.convertToJson(showChild),b.getChild(), showChild);
            }
        }
        parent.put("child", childArray);
        return parent;
    }

    public Object clone() {
        Branch b = null;
        try{
            b = (Branch)super.clone();
        }catch(CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return b;
    }

    /**
     * 获得节点列表。从父节点到自身。
     *
     * @return
     */
    public List<Branch> getNodeList() {
        LinkedList<Branch> list = new LinkedList<Branch>();
        Branch node = this;
        while (node != null) {
            list.addFirst(node);
            node = node.getParent();
        }
        return list;
    }

    /**
     * 获得节点列表ID。从父节点到自身。
     *
     * @return
     */
    public Integer[] getNodeIds() {
        List<Branch> branches = getNodeList();
        Integer[] ids = new Integer[branches.size()];
        int i = 0;
        for (Branch c : branches) {
            ids[i++] = c.getId();
        }
        return ids;
    }

    /**
     * 获得深度
     *
     * @return 第一层为0，第二层为1，以此类推。
     */
    public int getDeep() {
        int deep = 0;
        Branch parent = getParent();
        while (parent != null) {
            deep++;
            parent = parent.getParent();
        }
        return deep;
    }

    public Branch getTopBranch() {
        Branch parent = getParent();
        while (parent != null) {
            if (parent.getParent() != null) {
                parent = parent.getParent();
            } else {
                break;
            }
        }
        return parent;
    }

    public void init() {
        if (getPriority() == null) {
            setPriority(10);
        }
    }

    @Override
    public String getLftName() {
        return DEF_LEFT_NAME;
    }

    @Override
    public String getRgtName() {
        return DEF_RIGHT_NAME;
    }

    @Override
    public String getParentName() {
        return DEF_PARENT_NAME;
    }

    public Integer getParentId() {
        Branch parent = getParent();
        if (parent != null) {
            return parent.getId();
        } else {
            return null;
        }
    }

    public String getTreeCondition() {
        return "bean.site.id=" + getSite().getId();
    }

    public JSONObject chooseBranchToJson(){
        JSONObject province = new JSONObject();
        //取顶级组织架构
        if(getParentId() == null){
            JSONObject json = new JSONObject();
            json.put("id", getId());
            json.put("name", getName());
            json.put("city", "[]");
            province = json;
        //取二级组织架构
        }else if(getParent().getParentId() == null){
            JSONArray city = new JSONArray();
            JSONObject json2 = new JSONObject();
            json2.put("id", getId());
            json2.put("name", getName());
            json2.put("area", "[]");
            city.put(json2);

            JSONObject json = new JSONObject();
            json.put("id", getParentId());
            json.put("name", getParent().getName());
            json.put("city", city);
            province = json;
            //取3级组织架构
        }else if(getParent().getParent().getParentId() == null){
            JSONObject json3 = new JSONObject();
            json3.put("id", getId());
            json3.put("pid", getParentId());
            json3.put("name", getName());

            JSONArray city = new JSONArray();
            JSONObject json2 = new JSONObject();
            json2.put("id", getParentId());
            json2.put("name", getParent().getName());
            json2.put("area", json3);
            city.put(json2);

            JSONObject json = new JSONObject();
            json.put("id", getParent().getParentId());
            json.put("name", getParent().getParent().getName());
            json.put("city", city);
            province = json;
        }

        return province;
    }

}
